package com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.segments;

import static com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.enums.SqlKeyword.ORDER_BY;
import static java.util.stream.Collectors.joining;

import java.util.List;

import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.ISqlSegment;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.toolkit.StringPool;

/**
 * <p>
 * Order By SQL 片段
 * </p>
 *
 * @author Start
 */
@SuppressWarnings("serial")
public class OrderBySegmentList extends AbstractiSegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment) {
        list.remove(0);
        if (!isEmpty()) {
            super.add(() -> StringPool.COMMA);
        }
        return true;
    }

    @Override
    public String getSqlSegment() {
        if (isEmpty()) {
            return StringPool.EMPTY;
        }
        return this.stream().map(ISqlSegment::getSqlSegment).collect(joining(StringPool.SPACE,
            StringPool.SPACE + ORDER_BY.getSqlSegment() + StringPool.SPACE, StringPool.EMPTY));
    }
}
